import React, { Component } from 'react';

import Header from '../../core/components/header';
import Table from '../../core/components/table';
import Dialog from '../../core/utils/dialog';
import { Post } from '../../core/utils/fetch';
import {
    Link
} from 'react-router-dom';

import BatchEdit from './components/batch';

class Car extends Component {

    static defaultProps = {
    };

    state = {
    };

    render() {
        let columns = [
            { title: '序号', name: 'id' },
            { title: '车牌号', name: 'carNo' },
            { title: '品牌型号', name: 'carType.value' },
            { title: '车辆归属地', name: 'location.value' },
            { title: '车身颜色', name: 'color.value' },
            { title: '发动机号码', name: 'engineNo' },
            { title: '车辆状态', render: this.renderCarStatus },
            { title: '所属公司', name: 'comName' },
            { title: '操作', render: this.renderOpertion },
        ];
        return (
            <div>
                <Header title="车辆管理" subTitle="Cars  Manage"></Header>
                <Table
                    ref="table"
                    title="车辆列表"
                    subTitle={[
                        <Link to={{ pathname: "/car-edit" }} className="btn btn-sm btn-primary btn-add" style={{ "right": "100px" }}>新增</Link>,
                        <button type="button" className="btn btn-sm btn-primary btn-add" onClick={this._onImport}>导入</button>
                    ]}
                    isGet={true}
                    url={`/spi/custInvest/listCar`}
                    columns={columns}>
                </Table>

                <BatchEdit isShow={this.state.showEdit} onEdit={this._refresh} />
            </div >
        );
    }


    renderCarStatus = (dataItem) => {
        switch (dataItem.carStatus) {
            case "DRIVERING":
                return "运营中";
            case "OUT_DRIVEN":
                return "停运";
            default:
                return `未知车型-${dataItem.carStatus}`
        }
    };

    renderOpertion = (dataItem) => {
        return (
            <div>
                <Link to={{ pathname: "/car-edit", search: `?id=${dataItem.id}` }}>编辑</Link>
                {'    '}
                <a onClick={() => this._onDel(dataItem.id)}>删除</a>
            </div>
        );
    }

    _onImport = () => {
        this.setState({ showEdit: true });
    };

    _onDel = (id) => {
        Post(`/spi/custInvest/carDel/${id}`)
            .then(() => {
                Dialog.alert('删除成功', this._refresh);
            })
    };

    _refresh = () => {
        this.refs.table.refresh();
    };
}

export default Car;